package com.example.XueCuApp.View;

import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.XueCuApp.Base.BaseActivity;
import com.example.XueCuApp.Base.BaseConfig;
import com.example.XueCuApp.Bean.JJHaddOrRelieveRequsetBean;
import com.example.XueCuApp.Bean.JJHaddOrRelieveResultBean;
import com.example.XueCuApp.Bean.JJHterminalComputerRequestBean;
import com.example.XueCuApp.Bean.JJHterminalComputerResultBean;
import com.example.XueCuApp.Customer.CheckResultem;
import com.example.XueCuApp.R;
import com.example.XueCuApp.RetrufitHelp.BaseSubscriber;
import com.example.XueCuApp.RetrufitHelp.ExceptionHandle;
import com.example.XueCuApp.RetrufitHelp.RetrofitClient;
import com.example.XueCuApp.Utils.GetDeviceId;
import com.example.XueCuApp.Utils.SPUtil;
import com.example.XueCuApp.Utils.StringUtil;
import com.example.XueCuApp.Utils.ThreadManager;
import com.example.XueCuApp.Utils.ToastUtil;


public class SettingActivity extends BaseActivity {

    private Button btChange;
    private TextView bingShow;
    private Button btUnbing;
    private CheckResultem pointName;
    private CheckResultem locationName;
    private CheckResultem meationName;
    private Button save;
    private Button back;
    private CheckBox oneOne;
    private CheckBox oneMore;

    private JJHaddOrRelieveRequsetBean addBean;
    private String DeviceId;
    private String terminalNo;
    private Long mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void setUnBindingMeation() {

        RetrofitClient.getInstance(SettingActivity.this, BaseUrl, headers).JJHaddOrRelieveTerminalBind(addBean, new BaseSubscriber<JJHaddOrRelieveResultBean>(SettingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
            }

            @Override
            public void onNext(JJHaddOrRelieveResultBean jjHaddOrRelieveResultBean) {
                if (jjHaddOrRelieveResultBean.getCode().equals("200")) {
                    //去删除 保存的信息
                    SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_POINT, 0L);
                    SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_POINT_NAME, "");
                    SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_LOCATION, 0L);
                    SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_LOCATION_NAME, "");
                    SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_MEATION, 0L);
                    SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_MEATION_NAME, "");
                    SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_GATENO, "");

                    bingShow.setText("未绑定");
                    btChange.setText("绑定");
                    pointName.setResultText("");
                    locationName.setResultText("");
                    meationName.setResultText("");
                } else {
                    ToastUtil.showToast(jjHaddOrRelieveResultBean.getMessage());
                }
            }
        });

    }

    @Override
    protected void initView() {

        if (addBean == null) {
            addBean = new JJHaddOrRelieveRequsetBean();
        }

        //获取设备唯一标识
        ThreadManager.getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    DeviceId = GetDeviceId.getDeviceId(SettingActivity.this);
                    //获取保存在sd中的 设备唯一标识符
                    String readDeviceID = GetDeviceId.readDeviceID(SettingActivity.this);
                    //获取缓存在  sharepreference 里面的 设备唯一标识
                    String string = (String) SPUtil.get(SettingActivity.this, BaseConfig.Device_id, "");
                    //判断 app 内部是否已经缓存,  若已经缓存则使用app 缓存的 设备id
                    if (string != null && !string.equals("")) {
                        // 说明有保存，那么久将其放出去呗
                        if (readDeviceID.equals(string)) {
                            //缓存和文件地址一样，直接返回出去
                            DeviceId = readDeviceID;
                        }
                    } else {
                        DeviceId = GetDeviceId.getDeviceId(SettingActivity.this);
                        GetDeviceId.saveDeviceID(DeviceId, SettingActivity.this);
                    }

                    //最后再次更新app 的缓存
                    SPUtil.put(SettingActivity.this, BaseConfig.Device_id, DeviceId);
                    addBean.setTerminalComputerCpuId(DeviceId);

                    System.out.println("zyf :" + DeviceId);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
//                            addBean.setTerminalComputerCpuId(DeviceId);
                            checkTerminalComputerCpuId(DeviceId);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


        bingShow = (TextView) findViewById(R.id.tv_bind_show);
        btChange = (Button) findViewById(R.id.bt_setting_change);
        btUnbing = (Button) findViewById(R.id.bt_unbing);
        pointName = (CheckResultem) findViewById(R.id.cl_point_name);
        locationName = (CheckResultem) findViewById(R.id.cl_location_name);
        meationName = (CheckResultem) findViewById(R.id.cl_meation_name);
        save = (Button) findViewById(R.id.bt_save);
        back = (Button) findViewById(R.id.bt_back);

        oneOne = (CheckBox) findViewById(R.id.cb_one_one);
        oneMore = (CheckBox) findViewById(R.id.cb_one_more);

        int countNumber = (int) SPUtil.get(SettingActivity.this,  BaseConfig.JJH_CHECK_CHECKCOUNT, -1);
        if (countNumber == 1) {
            oneOne.setChecked(true);
            oneMore.setChecked(false);
        } else if (countNumber == -1) {
            oneOne.setChecked(false);
            oneMore.setChecked(true);
        } else {
            oneOne.setChecked(false);
            oneMore.setChecked(true);
        }

//        Long pName = (Long) SPUtil.get(SettingActivity.this, BaseConfig.JJH_SP_NAME, BaseConfig.JJH_CHECK_POINT, 0L);
//        String pNameStr = (String) SPUtil.get(SettingActivity.this, BaseConfig.JJH_SP_NAME, BaseConfig.JJH_CHECK_POINT_NAME, "");
//        Long lName = (Long) SPUtil.get(SettingActivity.this, BaseConfig.JJH_SP_NAME, BaseConfig.JJH_CHECK_LOCATION, 0L);
//        String lNameStr = (String) SPUtil.get(SettingActivity.this, BaseConfig.JJH_SP_NAME, BaseConfig.JJH_CHECK_LOCATION_NAME, "");
        mName = (Long) SPUtil.get(SettingActivity.this,  BaseConfig.JJH_CHECK_MEATION, 0L);
//        String mNameStr = (String) SPUtil.get(SettingActivity.this, BaseConfig.JJH_SP_NAME, BaseConfig.JJH_CHECK_MEATION_NAME, "");

        // 日照馆项目 只有一票一客
        oneOne.setVisibility(View.GONE);
        oneMore.setVisibility(View.GONE);

    }

    @Override
    protected void initData() {

        btChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(SettingActivity.this, BindingActivity.class), 201);
            }
        });

        btUnbing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //去解绑数据
                addBean.setTerminalSetFlag(1 + "");
                addBean.setTerminalComputerName("手持机");
                addBean.setTerminalId(mName + "");
                setUnBindingMeation();
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        oneOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oneOne.setChecked(true);
                oneMore.setChecked(false);
                // 一票一客  是  1
                SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_CHECKCOUNT, 1);
            }
        });

        oneMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oneOne.setChecked(false);
                oneMore.setChecked(true);
                // 一票多客是 -1
                SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_CHECKCOUNT, -1);
            }
        });
    }

    private void checkTerminalComputerCpuId(String deviceId) {
        iosLoadDialog.show();
        JJHterminalComputerRequestBean jjHterminalComputerRequestBean = new JJHterminalComputerRequestBean();
        jjHterminalComputerRequestBean.setTerminalComputerCpuId(deviceId);
        RetrofitClient.getInstance(SettingActivity.this, BaseUrl, headers).JJHfindTerminalInfoByComputerCpuId(jjHterminalComputerRequestBean, new BaseSubscriber<JJHterminalComputerResultBean>(SettingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
//                ToastUtil.showToast(e.code + e.message);

                SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_POINT, 0L);
                SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_POINT_NAME, "");
                SPUtil.put(SettingActivity.this, BaseConfig.JJH_CHECK_LOCATION, 0L);
                SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_LOCATION_NAME, "");
                SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_MEATION, 0L);
                SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_MEATION_NAME, "");
                SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_GATENO, "");

                iosLoadDialog.dismiss();
                bingShow.setText("未绑定");
                btChange.setText("绑定");
                pointName.setResultText("");
                locationName.setResultText("");
                meationName.setResultText("");

            }

            @Override
            public void onNext(JJHterminalComputerResultBean jjHterminalComputerResultBean) {
                if (jjHterminalComputerResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    terminalNo = jjHterminalComputerResultBean.getData().get(0).getTerminalNo();
                    // 保存  绑定机器
                    SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_GATENO, terminalNo);
                    SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_MEATION, jjHterminalComputerResultBean.getData().get(0).getTerminalId());
                    mName = jjHterminalComputerResultBean.getData().get(0).getTerminalId();
                    bingShow.setText("已绑定");
                    btChange.setText("修改");
                    pointName.setResultText(jjHterminalComputerResultBean.getData().get(0).getScenicSpotsName());
                    locationName.setResultText(jjHterminalComputerResultBean.getData().get(0).getPointShortName());
                    meationName.setResultText(jjHterminalComputerResultBean.getData().get(0).getTerminalShortName());

                } else {

                    SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_POINT, 0L);
                    SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_POINT_NAME, "");
                    SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_LOCATION, 0L);
                    SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_LOCATION_NAME, "");
                    SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_MEATION, 0L);
                    SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_MEATION_NAME, "");
                    SPUtil.put(SettingActivity.this,  BaseConfig.JJH_CHECK_GATENO, "");

                    iosLoadDialog.dismiss();
                    bingShow.setText("未绑定");
                    btChange.setText("绑定");
                    pointName.setResultText("");
                    locationName.setResultText("");
                    meationName.setResultText("");
                    ToastUtil.showToast(jjHterminalComputerResultBean.getMessage());
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 202) {
            if (requestCode == 201) {
                Long pName = (Long) SPUtil.get(SettingActivity.this,  BaseConfig.JJH_CHECK_POINT, 0L);
                String pNameStr = (String) SPUtil.get(SettingActivity.this,  BaseConfig.JJH_CHECK_POINT_NAME, "");
                Long lName = (Long) SPUtil.get(SettingActivity.this,  BaseConfig.JJH_CHECK_LOCATION, 0L);
                String lNameStr = (String) SPUtil.get(SettingActivity.this,  BaseConfig.JJH_CHECK_LOCATION_NAME, "");
                Long mName = (Long) SPUtil.get(SettingActivity.this,  BaseConfig.JJH_CHECK_MEATION, 0L);
                String mNameStr = (String) SPUtil.get(SettingActivity.this,  BaseConfig.JJH_CHECK_MEATION_NAME, "");
                checkTerminalComputerCpuId(DeviceId);
                if (StringUtil.isNullOrEmpty(pName + "")) {
                    // 如果 数据是空
                    bingShow.setText("未绑定");
                    pointName.setResultText("");
                    locationName.setResultText("");
                    meationName.setResultText("");
                } else {
                    bingShow.setText("已绑定");
                    pointName.setResultText(pNameStr + " " + pNameStr);
                    locationName.setResultText(lNameStr + " " + lName);
                    meationName.setResultText(mNameStr + " " + mName);

                }
            }
        }
    }

    @Override
    public int initLayoutId() {
        return R.layout.activity_setting;
    }

}
